package handle_db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.News;
import entities.News_Reply;

public class News_DB_Connector {
	private int news_id;
	private int owner;
	private String title;
	private ArrayList<News_Reply> replies;

	private DB_Connector db_Connector;
	private int resultSum;
	private ResultSet rs;

	private News news;

	News_DB_Connector() {
		db_Connector = new DB_Connector();
	}

	News get_News(int news_id) {
		String news_sql = "SELECT FROM news " + "WHERE news.news_id=" + news_id;
		rs = db_Connector.executeQuery(news_sql);
		try {
			while (rs.next()) {
				news.setNews_type(rs.getInt("news_type"));
				news.setOwner(rs.getInt("owner"));
				news.setIf_replied(rs.getBoolean("if_replied"));
				if (news.isIf_replied()) {
					// TODO news.setReplies(...);
				}
				news.setTitle(rs.getString("news_title"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		db_Connector.close();
		return news;
	}

	void set_News(News news) {
		String sql = "UPDATE news " + "SET news_type="
				+ news.getNews_type() + " , owner=" + news.getOwner()
				+ " , if_replied=" + news.isIf_replied() + " , news_title"
				+ news.getTitle() + "WHERE news.news_id=" + news.getNews_id();
		resultSum = db_Connector.executeUpdate(sql);
		//TODO set news' replies
	}
	
	void add(News news) {
		String sql = "INSERT INTO news"
			+"SET news_id="+news.getNews_id();
		resultSum = db_Connector.executeUpdate(sql);
		
		set_News(news);
	}
	
	void delete(int news_id) {
		String sql = "DELETE FROM news"
			+"WHERE news_id="+news_id;
		resultSum = db_Connector.executeUpdate(sql);
	}
}
